package com.example.basemvp.util;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by CXX on 2015/9/7.
 */
public class StringUtils {

    public static boolean isTel(String phoneNumber) {
        String expression = "((^(1)[0-9][0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);

        return matcher.matches();
    }

    public static boolean isNewVersion(String oldVer, String newVer) {
        if (TextUtils.isEmpty(oldVer) || TextUtils.isEmpty(newVer))
            return false;
        try {
            String[] oldStr = oldVer.split("\\.");
            String[] newStr = newVer.split("\\.");

            if (Integer.parseInt(newStr[0]) == Integer.parseInt(oldStr[0])) {
                if (Integer.parseInt(newStr[1]) == Integer.parseInt(oldStr[1])) {
                    return Integer.parseInt(newStr[2]) > Integer.parseInt(oldStr[2]);
                } else {
                    return Integer.parseInt(newStr[1]) > Integer.parseInt(oldStr[1]);
                }
            } else {
                return Integer.parseInt(newStr[0]) > Integer.parseInt(oldStr[0]);
            }

        } catch (Exception e) {

        }
        return false;
    }

    public static String getFormatNumForSensor(double num)//ddddd
    {
        return String.valueOf(Math.round(num));
    }

    public static boolean isCphm(String cph) {

        Pattern pattern = Pattern.compile("^[\u4e00-\u9fa5|WJ]{1}[A-Z0-9]{6}$");
        Matcher matcher = pattern.matcher(cph);
        if (!matcher.matches()) {
            return false;
        } else {
            return true;
        }
    }

    public static String formatNum(double num) {
        String format = (double) Math.round(num * 10) / 10 + "";
        if (format.substring(format.length() - 2, format.length()).equals(".0")) {
            format = format.substring(0, format.length() - 2);
        }
        return format;
    }

    public static String formatNumTwo(double num) {
        String format = (double) Math.round(num * 100) / 100 + "";
        if (format.substring(format.length() - 3, format.length()).equals(".00")) {
            format = format.substring(0, format.length() - 3);
        }
        if ("".equals(format)) {
            return "0";
        }
        return format;
    }


    public static String getText(String text) {
        if (TextUtils.isEmpty(text)) return "";
        return text;
    }

    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isEmpty(String value) {
        if (value != null && !"".equalsIgnoreCase(value.trim()) && !"null".equalsIgnoreCase(value.trim())) {
            return false;
        } else {
            return true;
        }
    }

    public static String getNumber(int number) {
        if (number < 10) {
            return "0" + number;
        } else {
            return String.valueOf(number);
        }
    }

    public static boolean isEquals(String... agrs) {
        String last = null;
        for (int i = 0; i < agrs.length; i++) {
            String str = agrs[i];
            if (isEmpty(str)) {
                return false;
            }
            if (last != null && !str.equalsIgnoreCase(last)) {
                return false;
            }
            last = str;
        }
        return true;
    }

    public static int randomDigit() {
        Random random = new Random();
        return random.nextInt(10);
    }

    public static String getNumberFromString(String s) {
        String regex = "(\\d*[.]\\d*)|(\\d+)";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        while (m.find()) {
            if (!"".equals(m.group()))
                return m.group();
        }
        return "";
    }

    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String formatDate(Date date, String format) {
        SimpleDateFormat ft = new SimpleDateFormat(format);
        return ft.format(date);
    }

    //yyyy-MM-dd hh:mm
    public static boolean compareTime(String oldTime, String newTime) {
        int oldY = Integer.valueOf(oldTime.substring(0, 4));
        int newY = Integer.valueOf(newTime.substring(0, 4));
        if (newY > oldY) {
            return true;
        } else if (newY < oldY) {
            return false;
        }
        int oldM = Integer.valueOf(oldTime.substring(5, 7));
        int newM = Integer.valueOf(newTime.substring(5, 7));
        if (newM > oldM) {
            return true;
        } else if (newM < oldM) {
            return false;
        }
        int oldD = Integer.valueOf(oldTime.substring(8, 10));
        int newD = Integer.valueOf(newTime.substring(8, 10));
        if (newD > oldD) {
            return true;
        } else if (newD < oldD) {
            return false;
        }
        int oldH = Integer.valueOf(oldTime.substring(11, 13));
        int newH = Integer.valueOf(newTime.substring(11, 13));
        if (newH > oldH) {
            return true;
        } else if (newH < oldH) {
            return false;
        }
        int oldm = Integer.valueOf(oldTime.substring(14, 16));
        int newm = Integer.valueOf(newTime.substring(14, 16));
        if (newm > oldm) {
            return true;
        } else if (newm < oldm) {
            return false;
        }
        return false;
    }
}
